package com.eh.erd.core.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.eh.erd.core.listener.ExcelDictDTOListener;
import com.eh.erd.core.pojo.dto.ExcelDictDTO;
import com.eh.erd.core.pojo.entity.Dict;
import com.eh.erd.core.mapper.DictMapper;
import com.eh.erd.core.service.DictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author zhuyuanwei
 * @since 2021-04-29
 */
@Slf4j
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importData(InputStream inputStream) {
        EasyExcel.read(inputStream, ExcelDictDTO.class,new ExcelDictDTOListener(baseMapper)).sheet().doRead();
    }

    @Override
    public List<ExcelDictDTO> listDictData() {

        List<Dict> dictList = baseMapper.selectList(null);
        ArrayList<ExcelDictDTO> excelDictDTOArrayList=new ArrayList<ExcelDictDTO>(dictList.size());
        dictList.forEach(item->{
            ExcelDictDTO excelDictDTO=new ExcelDictDTO();
            BeanUtils.copyProperties(item,excelDictDTO);
            excelDictDTOArrayList.add(excelDictDTO);
        });

        return excelDictDTOArrayList;
    }

    @Override
    public List<Dict> listByParentId(Long parentId) {
        try {
            List<Dict> dataList = (List<Dict>) redisTemplate.opsForValue().get("erd:core:dataList:"+parentId);
            if(dataList!=null){
                log.info("缓存命中");
                return dataList;
            }

        }catch (Exception e){
            log.error("redis服务器异常:"+ ExceptionUtils.getStackTrace(e));
        }
        synchronized (this){
            List<Dict> dataList = (List<Dict>) redisTemplate.opsForValue().get("erd:core:dataList:"+parentId);
            if(dataList!=null){
                log.info("缓存命中");
                return dataList;
            }
            log.info("缓存没命中");
            QueryWrapper<Dict> queryWrapper=new QueryWrapper<Dict>();
            queryWrapper.eq("parent_id",parentId);
            List<Dict> dictList = baseMapper.selectList(queryWrapper);
            List<Dict> dicts = dictList.stream().map(item -> {
                Integer count = baseMapper.selectCount(queryWrapper);
                item.setHasChildren(count.intValue() > 0 ? true : false);
                return item;
            }).collect(Collectors.toList());
            try {
                redisTemplate.opsForValue().set("erd:core:dataList:"+parentId,dicts,5, TimeUnit.MINUTES);
            }catch (Exception e){
                log.error("redis服务器异常:"+ ExceptionUtils.getStackTrace(e));
            }
            return dicts;
        }
    }
}
